package com.wwj.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wwj.springcloud.entity.Dept;
import com.wwj.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 服务熔断
@RestController
public class DeptController {

    @Autowired
    private DeptService deptServiceImpl;
    // 获取一些配置信息，得到具体的微服务
    @Autowired
    private DiscoveryClient discoveryClient;

    // http://localhost:8001/dept/get/1000
    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGetDeptById") //如果方法失败则调用hystrixGetDeptById()
    public Dept getDeptById(@PathVariable("id") Long deptNo) {
        Dept dept = deptServiceImpl.queryById(deptNo);
        if (dept == null) {
            throw new RuntimeException("id=>" + deptNo + ",不存在用户");
        }
        return dept;
    }

    public Dept hystrixGetDeptById(@PathVariable("id") Long deptNo) {
        Dept dept = new Dept();
        dept.setDeptNo(deptNo);
        dept.setName("id=>" + deptNo + "没有对应信息，null--@Hystrix");
        dept.setDbName("no this database in MySQL");
        return dept;
    }


}
